﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Web
{
    public class HomeController : CustomBaseController
    {
        private IUserService us;

        public HomeController()
        {
            WebConfigProvider wcp = new WebConfigProvider();
            us = new UserService(wcp);
        }
        //
        // GET: /Home/
        [CustomIsActive(Flag = AuthoriseFlag.page_sms_send)]
        public ActionResult Index()
        {
            var user = us.GetUserItemByCIL(User.Identity.Name);
            if (user.IsActive != 4)
            {
                HomeModel hm = new HomeModel();
                hm.IsActive = user.IsActive;
                hm.UserID = user.UserID;
                return View(hm);
            }
            return RedirectToAction("index", "send");
        }

        public ViewResult Setting()
        {
            return View();
        }

        [HttpPost]
        [CustomIsActive(Flag = AuthoriseFlag.page_sms_send)]
        public ActionResult AskForActive(int id)
        {
            HomeModel hm = new HomeModel();

            var user = us.GetUserItemByCIL(User.Identity.Name);

            if (user.IsActive < 4 && user.IsActive != 0)
            {
                hm.ActiveMsg = App_GlobalResources.Resources.Controller_Home_Activated;
                return RedirectToAction("index", "home");
            }
            //if (user.IsActive == 2)
            //{
            //    hm.ActiveMsg = "您已经申请过了,请勿重复申请";
            //    return RedirectToAction("index", "home");
            //}
            //if (user.UserID != id)
            //{
            //    hm.IsActive = user.IsActive;
            //    hm.UserID = user.UserID;
            //    hm.ActiveMsg = "您只能申请激活自己的帐号";
            //    return RedirectToAction("index", "home");
            //}

            WebConfigProvider wcp = new WebConfigProvider();

            if (us.AskForActive(id, wcp.LocalHost))
            {
                user = us.GetUserItemByCIL(User.Identity.Name);
                if (user.IsActive == 2)
                {

                    hm.IsActive = user.IsActive;
                    hm.UserID = user.UserID;
                    hm.ActiveMsg = App_GlobalResources.Resources.Controller_Home_Sent;
                    return RedirectToAction("index", "home");
                }
            }
            return RedirectToAction("index", "home");
        }

        public ActionResult ErrorPage()
        {
            return View();
        }
    }
}
